package com.whsxt.servlet;

import com.whsxt.common.Constant;
import com.whsxt.common.Result;
import com.whsxt.pojo.User;
import com.whsxt.service.VisitLogService;
import com.whsxt.service.impl.VisitLogServiceImpl;
import com.whsxt.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @Author 武汉尚学堂
 */
@WebServlet("/log.do")
public class VisitLogServlet extends HttpServlet {

    VisitLogService visitLogService = new VisitLogServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String service = req.getParameter("service");
        try {
            //通过反射调用方法
            Method m = this.getClass().getDeclaredMethod(service, HttpServletRequest.class, HttpServletResponse.class);
            m.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 新增拜访记录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String customerId = req.getParameter("id");
        String name = req.getParameter("name");
        String visitTime = req.getParameter("visitTime");
        String descp = req.getParameter("descp");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute(Constant.CURRENT_USER_KEY);
        Integer userId = user.getId();
        String realname = user.getRealname();
        Result result = visitLogService.addVisitLog(userId, realname, customerId, name, visitTime, descp);
        RespUtil.writer(resp,result);
    }

}
